package com.sentrix.auto.printercenter.infrastructure.agentserver.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/***
 * {
 *         "printer": {
 *             "name": "UNIV on MFP14328078"
 *         },
 *         "options": {
 *             "bounds": null,
 *             "colorType": "color",
 *             "copies": 1,
 *             "density": 0,
 *             "duplex": false,
 *             "fallbackDensity": null,
 *             "interpolation": "bicubic",
 *             "jobName": null,
 *             "legacy": false,
 *             "margins": 0,
 *             "orientation": null,
 *             "paperThickness": null,
 *             "printerTray": null,
 *             "rasterize": false,
 *             "rotation": 0,
 *             "scaleContent": true,
 *             "size": null,
 *             "units": "in",
 *             "forceRaw": false,
 *             "encoding": null,
 *             "spool": {
 *
 *             }
 *         },
 *         "data": [
 *             {
 *                 "type": "pixel",
 *                 "format": "pdf",
 *                 "flavor": "file",
 *                 "data": "file:\/\/\/C:\/Program%20Files\/QZ%20Tray\/demo\/assets\/pdf_sample.pdf",
 *                 "options": {
 *                     "pageWidth": "",
 *                     "pageHeight": "",
 *                     "pageRanges": "",
 *                     "ignoreTransparency": false,
 *                     "altFontRendering": false
 *                 }
 *             }
 *         ]
 *     }
 */
@Data
public class PrintParam {
    private Printer printer;
    private Option options;
    private List<PrintData> data;

    public PrintParam(String printerName, String jobName) {
        this.printer = new Printer(printerName);
        this.data = new ArrayList<>();
        this.options = new Option();
        this.options.setJobName(jobName);
    }

    public void setOption(Option options) {
        String jobName = this.options.getJobName();
        this.options = options;
        this.options.setJobName(jobName);
    }
    public void setOption(){

    }

    /**
     * 添加打印数据
     *
     * @param type   raw, pixel
     * @param format pdf, html, png
     * @param flavor BASE64, FILE, HEX, PLAIN, XML;
     * @param data   打印内容
     */
    public void addData(String type, String format, String flavor, String data) {
        if (StringUtils.isBlank(type)) {
            type = "pixel";
        }
        this.data.add(new PrintData(type, format, flavor, data, null));
    }

    /**
     * 添加打印数据
     *
     * @param format 文件格式
     * @param flavor BASE64, FILE, HEX, PLAIN, XML;
     * @param data   打印内容
     */
    public void addData(String format, String flavor, String data) {
        addData("pixel", format, flavor, data);
    }


    @Data
    @AllArgsConstructor
    public static class Printer {
        private String name;
    }

    @Data
    public static class Option {
        /**
         *
         */
        private String bounds;
        /**
         * 彩色或黑白
         */
        private String colorType = "color";
        /**
         * 打印份数
         */
        private int copies = 1;
        /**
         * 打印质量
         */
        private int density = 0;
        /**
         * 是否双面打印
         */
        private boolean duplex = false;
        /**
         * 降级质量
         */
        private String fallbackDensity = null;
        /**
         * 插值算法
         */
        private String interpolation = "bicubic";
        /**
         * 作业名称
         */
        private String jobName = null;
        /**
         * 兼容模式
         */
        private boolean legacy = false;
        /**
         * 间隔
         */
        private int margins = 0;
        /**
         * 纸张方向
         */
        private String orientation = null;
        /**
         * 纸张厚度
         */
        private String paperThickness = null;
        /**
         * 纸张 tray
         */
        private String printerTray = null;
        /**
         * 是否使用图片转码
         */
        private boolean rasterize = false;
        /**
         * 旋转
         */
        private int rotation = 0;
        /**
         * 是否缩放内容
         */
        private boolean scaleContent = true;
        /**
         * 纸张大小
         */
        private String size = null;
        /**
         * 单位
         */
        private String units = "in";
        /**
         * 强制使用原始数据
         */
        private boolean forceRaw = false;
        /**
         * 编码
         */
        private String encoding = null;
        /**
         * 输出
         */
        private Spool spool = new Spool();
    }

    @Data
    public static class Spool {
        private String test;
    }

    @Data
    @AllArgsConstructor
    public static class PrintData {
        /**
         * Raw（原始指令 / 数据）指打印机的原始控制指令（如 ESC/POS 指令、ZPL 指令），是让打印机执行打印动作的 “命令代码”（如控制打印换行、绘制条码的指令）。
         * 像素（Pixel）在 “光栅打印” 场景中，指将图像、PDF 等内容拆解为的最小显示点。因为多数 POS 打印机是 “光栅设备”，需把内容转换为 “像素矩阵” 才能打印（例如一张 100×100 像素的图像，会被拆成 10000 个点逐个打印）。
         */
        private String type = "pixel";
        /**
         * 文件格式
         * pdf, html, png
         */
        private String format;
        /**
         * 打印内容解析器
         * BASE64, FILE, HEX, PLAIN, XML;
         */
        private String flavor = "file";
        /**
         * file:\\/\\/\\/C:\\\\Program%20Files\\\\QZ%20Tray\\\\demo\\\\assets\\\\pdf_sample.pdf
         */
        private String data;
        /**
         *
         */
        private PrintDataOptions options;

        @Data
        public static class PrintDataOptions {
            /**
             * 页宽
             */
            private String pageWidth;
            /**
             * 页高
             */
            private String pageHeight;
            /**
             * 页码
             */
            private String pageRanges;
            /**
             * 忽略透明度
             */
            private boolean ignoreTransparency = false;
            /**
             * 使用替代字体
             */
            private boolean altFontRendering = false;
        }
    }
}
